# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=readline
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_basever=8.2
_patchlevel=001
pkgver=${_basever}.${_patchlevel}
pkgrel=6
pkgdesc="MinGW port of readline for editing typed command lines (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         #"${MINGW_PACKAGE_PREFIX}-ncurses"
         "${MINGW_PACKAGE_PREFIX}-termcap"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
options=('staticlibs' 'strip')
license=('spdx:GPL-3.0-or-later')
url="https://cnswww.cns.cwru.edu/php/chet/readline/rltop.html"
source=("https://ftp.gnu.org/gnu/${_realname}/${_realname}-${_basever}.tar.gz"{,.sig}
        # Patch adopted from https://hg.octave.org/mxe-octave/file/41e50d658de0/src/readline-1-sigwinch.patch
        "0001-sigwinch.patch"
        # Patch adopted from https://hg.octave.org/mxe-octave/file/41e50d658de0/src/readline-2-event-hook.patch
        "0002-event-hook.patch"
        "0003-fd_set.patch"
        "0004-locale.patch")
if [ ${_patchlevel} -gt 00 ]; then
    for (( p=1; p<=$((10#${_patchlevel})); p++ )); do
        source=(${source[@]} https://ftp.gnu.org/gnu/${_realname}/${_realname}-${_basever}-patches/readline${_basever//./}-$(printf "%03d" $p){,.sig})
    done
fi
validpgpkeys=('7C0135FB088AAF6C66C650B9BB5869F064EA74AB')
sha256sums=('3feb7171f16a84ee82ca18a36d7b9be109a52c04f492a053331d7d1095007c35'
            'SKIP'
            '2b30dcb0804abb6e7e4f44cd119bddef94c1b1d7ebff43dda401e710eda2fd0f'
            '52ae0da2769afe2853e3ffad8ae2e22eb8a72ce3ee8f2fc9084cb4dc554b2813'
            '6c90a984519f6e5a201c3b107ec5a7319db2a4f7b30f48dd43c0964894fbf3a6'
            '72ed438ae142ba9d5498652dfdf4fb517265bcf9a67199b7c5b35cc24fa25b9e'
            'bbf97f1ec40a929edab5aa81998c1e2ef435436c597754916e6a5868f273aff7'
            'SKIP')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}/readline-${_basever}"
  # Apply upstream patches
  for (( p=1; p<=$((10#${_patchlevel})); p++ )); do
    msg "applying patch readline${_basever//./}-$(printf "%03d" $p)"
    patch -Np0 -i ${srcdir}/readline${_basever//./}-$(printf "%03d" $p)
  done

  apply_patch_with_msg \
    0001-sigwinch.patch \
    0002-event-hook.patch \
    0003-fd_set.patch \
    0004-locale.patch

  # Remove RPATH from shared objects (FS#14366)
  sed -i 's|-Wl,-rpath,$(libdir) ||g' support/shobj-conf
  # Fully spell out linker option
  sed -i 's|-Wl,--export-all |-Wl,--export-all-symbols |g' support/shobj-conf
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}
  export CFLAGS="$CFLAGS -DNEED_EXTERN_PC=1" # Use extern PC variable from libtermcap
  ../${_realname}-${_basever}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --enable-shared \
    --without-curses
  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make install DESTDIR="${pkgdir}"

  install -Dm644 "${srcdir}/${_realname}-${_basever}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
